package org.terrier.matching.models.scorefitting;
import java.io.*;

import java.text.*;
import java.util.ArrayList;

import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.demo.*;
import org.jfree.chart.plot.*;
public class InputAndOutput{
	int Graphcount = 0;
	
	public String[]  ReadQueryTerms(String StrAddr){
	    ArrayList<String> QueryMemory = new ArrayList<String>();
	      try{
				FileReader fr = new FileReader(StrAddr + "words.txt");
				BufferedReader br = new BufferedReader(fr);
	                  do{
	                  QueryMemory.add(br.readLine());
	                  }
	                  while(!QueryMemory.remove(null));
			}catch(IOException e){
				e.printStackTrace();
			}
			String[] QueryMemoryStr = new String[QueryMemory.size()]; 
	      return QueryMemory.toArray(QueryMemoryStr);
	}
	public void OutputMeanAndSigma(String StrAddr,int Version,int N,double[][] Parameter){
		try{
			FileWriter fw = new FileWriter(StrAddr + N+"_"+Version+"_MeanAndSigma.txt");
			//BufferedWriter bw = new BufferedWriter(fw);
			//String str = Double.toString(Parameter[10][0]);
			DecimalFormat df=new DecimalFormat(".###");
			for(int i = 0;i < Parameter.length;i++){
				fw.write(df.format(Parameter[i][0]));
				fw.write(" ");
				fw.write(df.format(Parameter[i][1]));
				fw.write("\r\n");
			}
			fw.close();
			}catch(IOException e){
				e.printStackTrace();
			}
	}
	public void Outputtest(String StrAddr){
		try{
			FileWriter fw = new FileWriter(StrAddr+"test.txt");
			fw.write(Integer.toString(3));
			fw.close();
			}catch(IOException e){
				e.printStackTrace();
			}
	}
	public void OutputDocLengthAndTf(String StrAddr,int N,int[][][] Tf,int[] docL){
		try{
			FileWriter fw = new FileWriter(StrAddr + N+"_" + "docL.txt");
			for(int i = 0;i < docL.length;i++){
				fw.write(Integer.toString(docL[i]));
				fw.write(" ");		
				fw.write("\r\n");
			}
			fw.close();
			}catch(IOException e){
				e.printStackTrace();
			}
		/*try{
				FileWriter fw = new FileWriter(StrAddr + N+"_" + "DocTf.txt");
				System.out.println("writeDocTf "+Tf[0].length+" "+Tf[1].length+" "+Tf[2].length);
				for(int i = 0;i < Tf[0].length;i++){
					
					for(int j = 0;j < Tf[2].length; j++){
					fw.write(Integer.toString(Tf[i][0][j]));
					System.out.println("writeDocL i,j"+i+" "+j+" ");
					fw.write(" ");
					}
					fw.write("\r\n");
					for(int j = 0;j < Tf[2].length; j++){
						fw.write(Integer.toString(Tf[i][1][j]));
						System.out.println("writeDocL i,j"+i+" "+j+" ");
						fw.write(" ");
						}
					fw.write("\r\n");
				}
				fw.close();
				}catch(IOException e){
					e.printStackTrace();
				}*/
	}
	public void OutputGraph(String StrAddr,double[] DataSet,String titleStr){
        /** MyChartDemo mycd = new MyChartDemo(titleStr);        
        JFreeChart chart = mycd.creatChart(titleStr,StrAddr, DataSet, 1000);
        try{
        	FileOutputStream fosJpg = new FileOutputStream(StrAddr);
        	ChartUtilities.writeChartAsJPEG(fosJpg,1f,chart, 800, 600,null);
        	}catch(IOException e1){
        		e1.printStackTrace();
        	}
        	*/

	}
	public void OutputDetail(String StrAddr,int[] DocLength,int[][] DocTf,String[] termMem){
		//Output the length and tf
		//Output the p and inf
		
	}
	public void OutputDetail_Tf(String StrAddr,int[] DataSet,String titleStr){
		
	}
	public void OutputDetail_L(String StrAddr,int[] DataSet,String titleStr){
		
	}
	public void OutputDetail_P(String StrAddr,int[] DataSet,String titleStr){
		
	}
	public void OutputDetail_INF1(String StrAddr,int[] DataSet,String titleStr){
		
	}
	public void OutputGraph(String StrAddr,int[] DataSet,String titleStr){
		/**
        MyChartDemo mycd = new MyChartDemo(titleStr); 
        double[] DataSet_double = new double[DataSet.length];
        for(int i = 0;i<DataSet.length;i++){
        	DataSet_double[i] = (double)DataSet[i];
        }
        JFreeChart chart = mycd.creatChart(titleStr,StrAddr, DataSet_double, 1000);
        try{
        	FileOutputStream fosJpg = new FileOutputStream(StrAddr);
        	ChartUtilities.writeChartAsJPEG(fosJpg,1f,chart, 800, 600,null);
        	}catch(IOException e1){
        		e1.printStackTrace();
        	}
        	*/

	}
}
